'use strict';

var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('log4js').getLogger('<%=app.name %>');


var app = express();
app.use(require('morgan')('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());{css}


/* route 注册 */
<% for(var i=0,l=resources.length;i<l;i++) { %>
app.use('<%=resources[i].prefix %>', require('./routes/<%=resources[i].name %>'));  
<% } %>


/* route 注册 */

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});



// create a new http server
app.set('port',  3000);
app.set('address','0.0.0.0');

http.createServer(app).
  listen(app.get('port'), app.get('host'),function(){
    logger.info('listening at  %s:%d', app.get('address'), app.get('port'));
});

